package com.chenyun.tenant;

import com.chenyun.common.event.TokenResolveEvent;
import com.chenyun.common.vo.SessionUser;
import com.chenyun.satoken.starter.util.SaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * Event：
 * Description:
 *
 * @author: Lonni
 * @date: 2023/12/13 0013 11:01
 */
@Component
@Slf4j
@ConditionalOnProperty(value = "chenyun.tenantEnable",havingValue = "true")
public class ListenerEvent implements Serializable {


    @EventListener
    public void TokenResolveEvent(TokenResolveEvent tokenResolveEvent) {
        log.info("监听到token解析事件,当前状态{}",tokenResolveEvent.isInit());
        if (tokenResolveEvent.isInit()) {
            SessionUser loginUser = SaUtil.getLoginUser();
            log.info("设置租户上下文:{}",loginUser.getClientId());
            TenantContext.me.setTenantId(loginUser.getClientId());
        } else {
            TenantContext.me.clear();
        }

    }

}



